package com.element.character.player;

import world.Area;
import world.World;

import android.util.Log;

import com.element.Element;
import com.element.character.Person;
import com.element.information.Gender;
import com.element.information.Race;
import com.element.information.Stats;
import com.element.information.Traits;
import com.element.item.ConsumableItem;
import com.element.item.Equipetory;
import com.element.item.Inventory;
import com.element.item.Item;
import com.element.priorities.PQueue;
import com.element.priorities.Priority;

public class Player extends Person {
	
	boolean init = true;
	
	// Stuff that the player needs
	public Area currentArea;
	public World world;
	
	public String name;
	public String bio;
	
	public PQueue priorityQueue;
	public Priority currentTask;
	
	// Important constants/variables
	private int constitutionHealthModifier = 5;
	
	public Player(World world){
		this.world = world;
	}
	
	public void update(){
		//initialize the PQ
		priorityQueue = PQueue.newPQueue();
		
		//update the character
		updateStats();
		
		//update priorities
		percept(); //update the PQ
		updatePriorities();
		
		//update AI / let the player do stuff
		updateAI();
		
		/* write some tuff to the log */
		Log.d("Player", this.currentTask.toString());
		Log.d("Player", this.currentTask.getAI().toString());
		
		
		/* do this stuff last */
		
		//PQ get ready for the garb
		priorityQueue = null;
		init = false;
	}
	

	public void updateStats(){
		calculateHealth();
		calculateHunger();
	}
	
	public void calculateHealth(){
		this.mainStats.healthMaximum = this.mainStats.constitution*constitutionHealthModifier;
	}
	
	public void calculateHunger(){
		if(this.mainStats.hungerCurrent >= 0){
			this.mainStats.hungerCurrent -= 1;
		}
	}
	
	public void updateAI(){
		currentTask.getAI().doTick();
		if(currentTask.getAI().isFinished()){
			currentTask.getAI().doWhenFinished();
		}
	}
	
	public void updatePriorities(){
		/* check if the highest valued prio on the PQ is more prioritized than the currentTask */
		Priority p;
		p = priorityQueue.peek();
		
		if(init){
			currentTask = p;
		}

		if(p != null){
			if(p.getValue() > currentTask.getValue()){
				if(currentTask.invokeWhenRemoved){
					currentTask.whenRemoved();
				}
				if(p.invokeInvokeFirstTime){
					p.invokeFirstTime();
					p.invokeInvokeFirstTime = false;
				}
				if(p.invokeWhenAdded){
					p.whenAdded();
				}
				currentTask = p;
			} else {
				currentTask.updateValueCurrentTask();
			}
		}
	}
	
	/* Updates the "priorityQueue = PQ" with the available prios.*/
	public void percept(){ 
		priorityQueue.addPriorities(this);
	}
		
	/* Makes this player into a default test character. */
	public void makeDefaultPlayer(){
		name = "Mike Default";
		bio = "Born and raised in the south. Learnt to swim very early.";
		race = new Race("human0", world);
		gender = new Gender("male0", world);
		currentArea = new Area("area0", world);
		mainStats = new Stats("stats0", world);
		mainTraits = new Traits();
		mainInventory = new Inventory();
		mainInventory.add(new ConsumableItem("consumableitem0", world));
	}
}
